import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createCluster, createClient } from 'redis';
import { RedisClientType } from 'redis/dist/lib/client';
import { RedisClusterType } from 'redis/dist/lib/cluster';


@Injectable()
export class RedisService {
    private cluster:RedisClusterType;
    public store:RedisClientType;
    constructor(private configService:ConfigService) {
        this.init()
    }

    private async init() {
        // this.cluster = createCluster({
        //     rootNodes : this.configService.get('redis')
        // })

        // this.cluster.on('error', err => {
        //     console.log('Redis Cluster Error', err)
        // })

        // await this.cluster.connect()


        this.store = createClient()
        


        await this.store.connect()

        console.log('redis success')
    }

    public set(key, value, options?:any) {
        if(typeof value == 'number') {
            value = value.toString()
        }
        this.store.set(key, value, options)
    }

    public get(key:string) {
        return this.store.get(key)
    }

    public del(key:string) {
        return this.store.del(key)
    }
}
